package com.situ.week2.day2;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ExceptionDemo {
    @Test
    public void test1() {
        //运行时异常：代码有问题
        //java.lang.ArithmeticException: / by zero
        //int num = 1 / 0;

        //编译时异常
        try {
            FileInputStream fileInputStream = new FileInputStream("a.txt");
            System.out.println("ExceptionDemo.test1 try");
        } catch (FileNotFoundException e) {
//            throw new RuntimeException(e);
            e.printStackTrace();
        } finally {
            System.out.println("finally");
        }
        System.out.println("ExceptionDemo.test1");
    }
    @Test
    public void test2() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test3() {
        try {
            show();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    public void show() throws ClassNotFoundException {
        Class.forName("com.mysql.jdbc.Driver");
    }
    @Test
    public void test4(){
        try {
            FileInputStream fileInputStream = new FileInputStream("a.txt");
            fileInputStream.read();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            System.out.println("finally");
        }
    }
    @Test
    public void test() {
        try {
            FileInputStream fileInputStream = new FileInputStream("a.txt");
            fileInputStream.read();
        } catch (IOException e) {
            e.printStackTrace();
        } /*顺序不能返catch (FileNotFoundException e) {
            e.printStackTrace();
        } */finally {
            System.out.println("finally");
        }
    }
    public void QuQian(double money) throws MeiQianException {
        if (money >= 1000) {
            throw new MeiQianException("钱不够");
        }
        System.out.println("钱够了");
    }
}
